package com.llb666.shop.items.infrastructure.persistence;

import com.alibaba.fastjson2.JSON;
import com.llb666.shop.items.domain.entity.ItemsSpec;
import com.llb666.shop.items.domain.repository.ItemsSpecRepository;
import com.llb666.shop.items.infrastructure.conver.ItemsSpecCvt;
import com.llb666.shop.items.infrastructure.mapper.ItemsSpecMapper;
import com.llb666.shop.items.infrastructure.po.ItemsSpecPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 商品规格仓储层实现类.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ItemsSpecRepositoryImpl implements ItemsSpecRepository {

    private final ItemsSpecMapper itemsSpecMapper;

    /**
     * 查询商品规格信息.
     *
     * @param itemId 商品ID
     * @return 商品规格信息
     */
    @Override
    public List<ItemsSpec> queryItemsSpecById(String itemId) {
        Example example = new Example(ItemsSpecPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return ItemsSpecCvt.INSTANCE.po2doList(itemsSpecMapper.selectByExample(example));
    }

    /**
     * 根据规格Id查询商品规格信息.
     *
     * @param itemSpecId 规格Id
     * @return 商品规格信息
     */
    @Override
    public ItemsSpec queryItemSpecById(String itemSpecId) {
        return ItemsSpecCvt.INSTANCE.po2do(itemsSpecMapper.selectByPrimaryKey(itemSpecId));
    }

    @Override
    public void decreaseItemSpecStock(String itemSpecId, int buyCounts) {
        int result = itemsSpecMapper.decreaseItemSpecStock(itemSpecId, buyCounts);
        if (result != 1) {
            throw new RuntimeException("订单创建失败，原因：库存不足!");
        }
    }

    /**
     * 根据规格Id查询商品规格信息.
     *
     * @param itemSpecIdList 规格Id列表
     * @return 商品规格信息列表
     */
    @Override
    @Async("itemsServiceExecutor")
    public CompletableFuture<List<ItemsSpec>> getItemSpecFutureByIds(List<String> itemSpecIdList) {
        Example example = new Example(ItemsSpecPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", itemSpecIdList);
        List<ItemsSpec> itemsSpecs = ItemsSpecCvt.INSTANCE.po2doList(itemsSpecMapper.selectByExample(example));
        return CompletableFuture.completedFuture(itemsSpecs);
    }

    /**
     * 根据规格列表查询商品规格列表信息.
     *
     * @param specList 规格列表id
     * @return 商品规格列表信息
     */
    @Override
    public List<ItemsSpec> getItemSpecByIds(String specList) {
        Example example = new Example(ItemsSpecPo.class);
        Example.Criteria criteria = example.createCriteria();
        List <String> itemSpecIdList = Arrays.asList(specList.split(",").clone());
        criteria.andIn("id", itemSpecIdList);
        return ItemsSpecCvt.INSTANCE.po2doList(itemsSpecMapper.selectByExample(example));
    }

}
